package com.zkt.practice.server.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.practice.api.common.Result;
import com.zkt.practice.api.req.*;
import com.zkt.practice.api.vo.RankVO;
import com.zkt.practice.api.vo.ReportVO;
import com.zkt.practice.api.vo.ScoreDetailVO;
import com.zkt.practice.api.vo.SubjectDetailVO;
import com.zkt.practice.server.service.PracticeDetailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/23
 * @description 练题详情controller层
 **/

@RestController
@Slf4j
@RequestMapping("/detail")
public class PracticeDetailController {
	
	@Resource
	private PracticeDetailService practiceDetailService;
	
	/**
	 * 提交练题情况
	 */
	@PostMapping(value = "submit")
	public Result<Boolean> submit(@RequestBody SubmitPracticeDetailReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("提交练题情况入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getSetId())) {
				throw new Exception("套题id不能为空");
			}
			if (ObjUtil.isNull(req.getPracticeId())) {
				throw new Exception("练习id不能为空");
			}
			if (StrUtil.isBlank(req.getSubmitTime())) {
				throw new Exception("交卷时间不能为空");
			}
			if (StrUtil.isBlank(req.getTimeUse())) {
				throw new Exception("用时不能为空");
			}
			
			Boolean result = practiceDetailService.submit(req);
			if (log.isInfoEnabled()) {
				log.info("提交练题情况出参{}", JSON.toJSONString(result));
			}
			
			return Result.ok(result);
		} catch (Exception e) {
			log.error("提交练题情况异常！错误原因{}", e.getMessage(), e);
			return Result.fail("提交练题情况失败");
		}
	}
	
	/**
	 * 提交题目
	 */
	@PostMapping(value = "/submitSubject")
	public Result<Boolean> submitSubject(@RequestBody SubmitSubjectDetailReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("练习提交题目入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getPracticeId())) {
				throw new Exception("练习id不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectId())) {
				throw new Exception("题目id不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectType())) {
				throw new Exception("题目类型不能为空");
			}
			if (StrUtil.isBlank(req.getTimeUse())) {
				throw new Exception("用时不能为空");
			}
			
			Boolean result = practiceDetailService.submitSubject(req);
			log.info("提交题目出参{}", result);
			
			return Result.ok(result);
		} catch (Exception e) {
			log.error("提交题目异常！错误原因{}", e.getMessage(), e);
			return Result.fail("提交题目失败");
		}
	}
	
	/**
	 * 答案解析-每题得分
	 */
	@PostMapping(value = "/getScoreDetail")
	public Result<List<ScoreDetailVO>> getScoreDetail(@RequestBody GetScoreDetailReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("每题得分入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getPracticeId())) {
				throw new Exception("练习id不能为空");
			}
			
			List<ScoreDetailVO> list = practiceDetailService.getScoreDetail(req);
			if (log.isInfoEnabled()) {
				log.info("每题得分出参{}", JSON.toJSONString(list));
			}
			
			return Result.ok(list);
		} catch (Exception e) {
			log.error("每题得分异常！错误原因{}", e.getMessage(), e);
			return Result.fail("答案解析-每题得分失败");
		}
	}
	
	/**
	 * 答案解析-答题详情
	 */
	@PostMapping(value = "/getSubjectDetail")
	public Result<SubjectDetailVO> getSubjectDetail(@RequestBody GetSubjectDetailReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("答题详情入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getPracticeId())) {
				throw new Exception("练习id不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectId())) {
				throw new Exception("题目id不能为空");
			}
			if (ObjUtil.isNull(req.getSubjectType())) {
				throw new Exception("题目类型不能为空");
			}
			
			SubjectDetailVO subjectDetailVO = practiceDetailService.getSubjectDetail(req);
			if (log.isInfoEnabled()) {
				log.info("答案详情出参{}", JSON.toJSONString(subjectDetailVO));
			}
			
			return Result.ok(subjectDetailVO);
		} catch (Exception e) {
			log.error("答案详情异常！错误原因{}", e.getMessage(), e);
			return Result.fail("答案解析-答题详情失败");
		}
	}
	
	/**
	 * 答案解析-评估报告
	 */
	@PostMapping(value = "/getReport")
	public Result<ReportVO> getReport(@RequestBody GetReportReq req) {
		try {
			if (log.isInfoEnabled()) {
				log.info("评估报告入参{}", JSON.toJSONString(req));
			}
			
			if (ObjUtil.isNull(req)) {
				throw new Exception("参数不能为空");
			}
			if (ObjUtil.isNull(req.getPracticeId())) {
				throw new Exception("练习id不能为空");
			}
			
			ReportVO reportVO = practiceDetailService.getReport(req);
			if (log.isInfoEnabled()) {
				log.info("评估报告出参{}", JSON.toJSONString(reportVO));
			}
			
			return Result.ok(reportVO);
		} catch (Exception e) {
			log.error("获取评估报告异常！错误原因{}", e.getMessage(), e);
			return Result.fail("获取评估报告失败");
		}
	}
	
	/**
	 * 获取练习榜
	 */
	@GetMapping(value = "/getPracticeRankList")
	public Result<List<RankVO>> getPracticeRankList() {
		try {
			List<RankVO> list = practiceDetailService.getPracticeRankList();
			if (log.isInfoEnabled()) {
				log.info("练习榜出参{}", list);
			}
			
			return Result.ok(list);
		} catch (Exception e) {
			log.error("练习榜报错！错误原因{}", e.getMessage(), e);
			return Result.fail("获取练习榜失败");
		}
	}
	
	/**
	 * 放弃练习
	 */
	@GetMapping(value = "/giveUp")
	public Result<Boolean> giveUp(@RequestParam("practiceId") Long practiceId) {
		try {
			if (log.isInfoEnabled()) {
				log.info("放弃练习入参{}", practiceId);
			}
			
			if (ObjUtil.isNull(practiceId)) {
				throw new Exception("练习id不能为空");
			}
			
			Boolean result = practiceDetailService.giveUp(practiceId);
			if (log.isInfoEnabled()) {
				log.info("放弃练习出参{}", result);
			}
			
			return Result.ok(result);
		} catch (Exception e) {
			log.error("放弃练习异常！错误原因{}", e.getMessage(), e);
			return Result.fail("放弃练习失败");
		}
	}
	
}
